package ihm;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import dao.MySqlConnection;
import dao.MySqlFramework;
import java.awt.Label;
import java.awt.Color;

@SuppressWarnings("serial")
public class Connecter extends JFrame implements ActionListener
{

	private JLabel lblIdentifiant;
	private JLabel lblMotDePasse;
	private JLabel lblModule;
	private JTextField txtUtil;
	private JTextField txtMdp;
	private JButton btnConnexion;
	private JButton btnQuitter;
	private Label lblInfosErreur;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try
				{
					Connecter frame = new Connecter();
					frame.setVisible(true);
				} catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the application / the frame
	 */
	public Connecter() 
	{
		setTitle("Connexion");
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 460, 315);
		
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		this.lblModule = new JLabel("Gestionnaire des entrainements");
		
		this.lblModule.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		this.lblModule.setBounds(111, 27, 246, 42);
		contentPane.add(this.lblModule);
		
		
		this.lblIdentifiant = new JLabel("Nom d'utilisateur :");
		this.lblIdentifiant.setBounds(68, 103, 116, 27);
		contentPane.add(this.lblIdentifiant);
		
		this.lblMotDePasse = new JLabel("Mot de passe :");
		this.lblMotDePasse.setBounds(68, 141, 116, 27);
		contentPane.add(this.lblMotDePasse);
		
		this.txtMdp = new JTextField();
		this.txtMdp.setBounds(177, 142, 110, 20);
		contentPane.add(this.txtMdp);
		
		this.txtUtil = new JTextField();
		this.txtUtil.setBounds(177, 102, 110, 20);
		contentPane.add(this.txtUtil);
		
		this.btnConnexion = new JButton("Connexion");
		this.btnConnexion.addActionListener(this);
		this.btnConnexion.setBounds(280, 200, 100, 20);
		contentPane.add(this.btnConnexion);
		
		this.btnQuitter = new JButton("Quitter");
		this.btnQuitter.addActionListener(this);
		this.btnQuitter.setBounds(80, 200, 100, 20);
		contentPane.add(this.btnQuitter);
		
		lblInfosErreur = new Label("Les champs : Login - Mot de passe  doivent \u00EAtre renseigner !");
		lblInfosErreur.setForeground(Color.RED);
		lblInfosErreur.setFont(new Font("Dialog", Font.ITALIC, 12));
		lblInfosErreur.setBounds(68, 246, 341, 20);
		lblInfosErreur.setVisible(false);
		contentPane.add(lblInfosErreur);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource() == this.btnQuitter)
		{
			this.dispose();
		}
		
		if(evt.getSource() == this.btnConnexion)
		{
			String identifiant = txtUtil.getText();
			String motDePasse = txtMdp.getText();
			int verif = 0;
			
			try 
			{
				
				MySqlConnection.getConnection("bdd_centre", "root", "");
				
				String sql = "select numAdherent, login, password, type " +
							"from Adherent " +
							"where login = '" + motAvecCote(identifiant) + "' and password = '" + motAvecCote(motDePasse) + "' ";
				ResultSet res = MySqlFramework.executerLecture(sql);
			
				while(!res.next());
				{	
					@SuppressWarnings("unused")
					int numero = res.getInt("numAdherent");
					
					String Login = res.getString("login");
					String Password = res.getString("password");
					String Type = res.getString("type");
					verif = verif +1;
					
					if(identifiant.isEmpty() || motDePasse.isEmpty())
					{
						lblInfosErreur.setVisible(true);
						btnConnexion.disable();
					}
					
					if(Login.equals(identifiant) && Password.equals(motDePasse))
					{					

						if(verif == 0)
						{
							JOptionPane.showMessageDialog(null, "Vérifier Login / Mot de passe", "Problème d'authentification",
									JOptionPane.ERROR_MESSAGE);
						}
						else
						{
							if(Type.equals("Moniteur"))
							{
								AccueilMoniteur frame = new AccueilMoniteur();
								frame.setVisible(true);
								this.dispose();
							}
							if(Type.equals("Responsable"))
							{
								AccueilResponsable frame = new AccueilResponsable();
								frame.setVisible(true);
								this.dispose();
							}
						}
					}
				}
				
			}
			catch (SQLException e1) 
			{
				e1.printStackTrace();
			}
			
			
		}
	}
	
	/**
	 * @param laChaine On récupère la variable qui est passé en paramêtre
	 * @return motFinal  On retourne la chaine de caractere entre cotes de 
	 * 					 la variable passée en paramètre pour l'utiliser 
	 * 					 dans une requete sql.
	 */
	public static String motAvecCote (String laChaine)
	{
		String caractere;
		String motFinal="";
		int i;
		
		for( i=0; i < laChaine.length(); i++ )
		{
			caractere = laChaine.substring(i, i+1);
			if (caractere.equals("'")==true)
			{
				caractere="''";
			}
	
			motFinal = motFinal + "" + caractere;
		}
		return motFinal;
	}
}
